package com.tencent.examples.core.processor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tencent.examples.blueking.mysql.model.JobStoreTime;
import com.tencent.examples.core.processor.common.BkobjidEnum;
import com.tencent.examples.core.processor.common.CommParams;
import com.tencent.examples.core.processor.dto.BkInstDto;
import com.tencent.examples.core.processor.dto.BkItopInstDto;
import com.tencent.examples.core.processor.dto.BkMqInstDto;
import com.tencent.examples.core.processor.dto.BkResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author :  CHENWEN
 * @Description : 蓝鲸解析器
 * @Creation Date:  2019/3/28
 */
@Component
public class BlueKingProcessor {
    @Autowired
    CommParams commParams;
    @Autowired
    InstConnect instConnect;


    /**
     * 接口实例解析
     *
     * @param
     */
    public List jkSearchProcessor(BkobjidEnum v) {
        List jkDtos = new ArrayList<>();

        JSONObject jsonObject = commParams.initJson();
        jsonObject.put("bk_obj_id", v.name());
        jsonObject.put("bk_supplier_account", "0");

        JSONObject result = instConnect.searchInst(jsonObject);
        if (result != null) {
            String data = result.getString("data");
            if (data != null) {
                String info = JSONObject.parseObject(data).getString("info");
                if (v == BkobjidEnum.jk) {
                    jkDtos = JSONObject.parseArray(info, BkMqInstDto.class);
                } else if (v == BkobjidEnum.itop) {
                    jkDtos = JSONObject.parseArray(info, BkItopInstDto.class);
                }
            }
        }

        return jkDtos;
    }

    /**
     * 查询实例
     *
     * @param
     */
    public List<BkInstDto> searchInstProcessor(String bk_obj_id, String field, String value) {
        List<BkInstDto> storeDto = new ArrayList<>();

        JSONObject jsonObject = commParams.initJson();
        jsonObject.put("bk_obj_id", bk_obj_id);
        jsonObject.put("bk_supplier_account", "0");
        JSONObject j = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONObject j1 = new JSONObject();
        j1.put("field", field);
        j1.put("operator", "$eq");
        j1.put("value", value);
        jsonArray.add(j1);
        j.put(bk_obj_id, jsonArray);
        jsonObject.put("condition", j);

        JSONObject result = instConnect.searchInst(jsonObject);
        if (result != null) {
            String data = result.getString("data");
            if (data != null) {
                String info = JSONObject.parseObject(data).getString("info");
                storeDto = JSONObject.parseArray(info, BkInstDto.class);
            }
        }

        return storeDto;
    }

    /**
     * 新增实例
     *
     * @return
     */
    public BkResult createInstProcessor(String bk_obj_id, ConcurrentHashMap<String, Object> bkMap) {
        JSONObject jsonObject = commParams.initJson();
        jsonObject.put("bk_obj_id", bk_obj_id);
        jsonObject.put("bk_supplier_account", "0");

        bkMap.forEach(jsonObject::put);

        JSONObject result = instConnect.createInst(jsonObject);
        if (result != null) {
            return result.toJavaObject(BkResult.class);
        }
        return null;
    }

    /**
     * 更新实例信息
     *
     * @param bkMap
     * @return
     */
    public BkResult updateInstProcessor(String bk_obj_id, ConcurrentHashMap<String, Object> bkMap) {
        JSONObject jsonObject = commParams.initJson();
        jsonObject.put("bk_obj_id", bk_obj_id);
        jsonObject.put("bk_supplier_account", "0");

        bkMap.forEach(jsonObject::put);

        JSONObject result = instConnect.updateInst(jsonObject);
        if (result != null) {
            return result.toJavaObject(BkResult.class);
        }
        return null;
    }

    /**
     * 更新门店闭店时间zabbix
     *
     * @return
     */
    public BkResult updateZabbixProcessor(JSONObject jsonObject) {
        JSONObject result = instConnect.updateZabbix(jsonObject);
        if (result != null) {
            return result.toJavaObject(BkResult.class);
        }
        return null;
    }
}
